<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\Address;
use Illuminate\Http\Request;


class AddressController extends Controller
{
    /**
     * 用户地址列表
     * @param Request $request
     * @return array
     */
    public function list(Request $request)
    {
        $user = $request->user();
        $list = Address::userList($user['id']);
        return makeResponse($list, 'success', 200);
    }

    /**
     * 用户添加地址
     * @param Request $request
     * @return array
     */
    // todo 需要记录 area_code
    public function create(Request $request)
    {
        $user = $request->user();

        $contacts = $request->input('contacts');
        $phone = $request->input('phone');
        $full_address = $request->input('full_address');
        $is_default = $request->input('is_default');
        $area_code = $request->input('area_code');
        $user_id = $user['id'];
        $province = $request->input('province');
        $city = $request->input('city');
        $area = $request->input('area');

        if ($is_default == 1) {
            $result = Address::where(['user_id' => $user_id])->update(['is_default' => 0]);
        } else {
            if (!Address::where('user_id',$user_id)->first()) {
                $is_default = 1;
            }
        }

        $model = Address::createOne(compact('contacts', 'phone', 'full_address', 'is_default', 'user_id', 'province',
            'city',
            'area', 'area_code'));

        if ($model->save()) {
            return makeResponse([], '添加地址成功～', 201);
        } else {
            return makeResponse([], '添加地址失败～', 500);
        }
    }

    public function edit(Request $request, $id)
    {
        $user = $request->user();

        $contacts = $request->input('contacts');
        $phone = $request->input('phone');
        $full_address = $request->input('full_address');
        $is_default = $request->input('is_default');
        $area_code = $request->input('area_code');
        $user_id = $user['id'];
        $province = $request->input('province');
        $city = $request->input('city');
        $area = $request->input('area');

        if ($is_default == 1) {
            $result = Address::where(['user_id' => $user_id])->update(['is_default' => 0]);
        }

        $model = Address::editOne(['id' => $id],
            compact('contacts', 'phone', 'full_address', 'is_default', 'user_id', 'province',
                'city',
                'area', 'area_code'));

        if ($model->save()) {
            return makeResponse([], '修改地址成功～', 201);
        } else {
            return makeResponse([], '修改地址失败～', 500);
        }
    }

    /**
     * 删除用户地址
     * @param Request $request
     * @param $id
     * @return array
     */
    public function delete(Request $request, $id)
    {
        $user = $request->user();
        $result = Address::where('id', $id)->where('user_id', $user['id'])->delete();
        if ($result) {
            return makeResponse([], '删除地址成功～', 204);
        } else {
            return makeResponse([], '删除地址失败～', 500);
        }
    }

    /**
     * 设置默认地址
     * @param Request $request
     * @param $addressId
     * @return array
     */
    public function setAddressDefault(Request $request)
    {
        $addressId = $request->input('id');
        $user = $request->user();
        $result = Address::setAddressDefault($addressId, $user['id']);
        if ($result) {
            return makeResponse([], '设置成功～', 200);
        } else {
            return makeResponse([], 'bad request', 500);
        }
    }

    /**
     * 地址详情
     * @param Request $request
     * @param $id
     * @return array
     */
    public function details(Request $request, $id)
    {
        $user = $request->user();
        $item = Address::details($id, $user['id']);
        return makeResponse($item, 'success', 200);
    }
}